@extends('admin.common.main')

@section('content')
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 房源中心 <span
            class="c-gray en">&gt;</span> 房源管理 <a class="btn btn-success radius r"
                                                  style="line-height:1.6em;margin-top:3px"
                                                  href="javascript:location.replace(location.href);" title="刷新"><i
                class="Hui-iconfont">&#xe68f;</i></a></nav>
    <div class="page-container">
        <form>
            <div class="text-c">
                <input type="text" class="input-text" style="width:250px" placeholder="输入房源名称" id=""
                       name="fang_name">
                <button type="submit" class="btn btn-success radius" id="" name=""><i class="Hui-iconfont">&#xe665;</i>
                    搜房源
                </button>
            </div>
        </form>
        <div class="cl pd-5 bg-1 bk-gray mt-20">
            <span class="l">
                <a href="javascript:;" onclick="datadel()"
                   class="btn btn-danger radius"><i
                        class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>
                <a href="{{route('admin.fang.create')}}"
                   class="btn btn-primary radius"><i
                        class="Hui-iconfont">&#xe600;</i> 添加房源</a>

            </span>
            <div class="mt-20">
                @include('admin.common.error')
                <table class="table table-border table-bordered table-hover table-bg table-sort">
                    <thead>
                    <tr class="text-c">
                        <th width="25"><input type="checkbox"></th>
                        <th width="20">ID</th>
                        <th width="200">房源名称</th>
                        <th width="90">房源小区名称</th>
                        <th width="150">房源地址</th>
                        <th width="150">房源状态</th>
                        <th width="130">加入时间</th>
                        <th width="100">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($data as $key=>$val)
                        @if(isset($val['_source']))
                            <tr class="text-c">
                                <td><input type="checkbox" value="{{$val['_source']['id']}}" name="id[]"></td>
                                <td>{{$val['_source']['id']}}</td>
                                <td>{!! $val['highlight']['fang_name'][0] !!}</td>
                                <td>{{$val['_source']['fang_xiaoqu']}}</td>
                                <td>{{$val['_source']['fang_addr']}}</td>
                                <td>

                                </td>
                                <td>{{$val['_source']['created_at']}}</td>
                                <td class="td-manage">
                                    <a title="删除" href=""
                                       class="label label-primary radius">删除</a>
                                    <a title="编辑" href="javascript:;" class="label label-primary radius">编辑</a>
                                </td>
                            </tr>
                        @else
                            <tr class="text-c">
                                <td><input type="checkbox" value="{{$val['id']}}" name="id[]"></td>
                                <td>{{$val['id']}}</td>
                                <td>{{$val['fang_name']}}</td>
                                <td>{{$val['fang_xiaoqu']}}</td>
                                <td>{{$val['fang_addr']}}</td>
                                <td>
                                    @if($val['fang_status'] == 0)
                                        <a class="btn btn-success radius" fang_id="{{$val['id']}}"
                                           fang_status="1"
                                           onclick="changStatus(this)">未出租</a>
                                    @else
                                        <a class="btn btn-warning radius" fang_id="{{$val['id']}}"
                                           fang_status="0"
                                           onclick="changStatus(this)">已出租</a>
                                    @endif
                                </td>
                                <td>{{$val['created_at']}}</td>
                                <td class="td-manage">
                                    <a title="删除" href=""
                                       class="label label-primary radius">删除</a>
                                    <a title="编辑" href="javascript:;" class="label label-primary radius">编辑</a>
                                </td>
                            </tr>
                        @endif
                    @endforeach
                    </tbody>
                </table>

            </div>

            @endsection

            @section('js')
                <script type="text/javascript">

                    /*用户-删除*/
                    $('.delbtn').click(function (obj) {
                        let url = $(this).attr('href');
                        let _that = $(this);
                        layer.confirm('确认要删除吗？', function () {
                            $.ajax({
                                type: 'DELETE',
                                data: {
                                    //这个token标识是_token
                                    _token: "{{csrf_token()}}"
                                },
                                url: url,
                                dataType: 'json',
                                success: function (data) {
                                    if (data.error_code == 0) {
                                        //删除成功移除当前行
                                        // _that.parents("tr").remove();
                                        //不移除当前行了，要展示恢复按钮
                                        history.go(0)
                                        //提示信息
                                        layer.msg('已删除!', {icon: 1, time: 1000});
                                    } else {
                                        layer.msg(data.msg);
                                    }

                                },
                                error: function (data) {
                                    console.log(data.msg);
                                },
                            });
                        });
                        return false;

                    });

                    $('.restorebtn').click(function (obj) {
                        let url = $(this).attr('href');
                        $.ajax({
                            type: 'GET',
                            url: url,
                            dataType: 'json',
                            success: function (data) {
                                if (data.error_code == 0) {
                                    history.go(0)
                                    //提示信息
                                    layer.msg('已恢复!', {icon: 1, time: 1000});
                                } else {
                                    layer.msg(data.msg);
                                }

                            },
                            error: function (data) {
                                console.log(data.msg);
                            },
                        });

                        return false;

                    });


                    function changStatus(evt) {
                        let fang_id = $(evt).attr('fang_id');
                        let fang_status = $(evt).attr('fang_status');
                        $.ajax({
                            url: "{{route('admin.fang.change_status')}}",
                            method: 'GET',
                            data: {
                                _token: "{{csrf_token()}}",
                                fang_id,
                                fang_status
                            },
                            success: ret => {
                                if (ret.error_code == 0) {
                                    if (fang_status == 1) {
                                        $(evt).removeClass('btn-success').addClass('btn-warning')
                                        $(evt).text('已出租')
                                        $(evt).attr('fang_status',0)
                                    } else {
                                        $(evt).removeClass('btn-warning').addClass('btn-success')
                                        $(evt).text('未出租')
                                        $(evt).attr('fang_status',1)
                                    }
                                }
                            }
                        })
                    }


                    /**
                     * 批量删除
                     */
                    function datadel() {


                        layer.confirm('您确定要批量删除吗？？', function () {

                            let ids = $('input[name="id[]"]:checked')
                            //存储要删除的id
                            let id = []

                            $.each(ids, (key, val) => {
                                //取dom节点value的值，取到要删除id,把要删除的数据追加打数组中
                                id.push(val.value)
                            })

                            $.ajax({
                                type: 'DELETE',
                                data: {
                                    //这个token标识是_token
                                    _token: "{{csrf_token()}}",
                                    id: id
                                },
                                url: "",
                                dataType: 'json',
                                success: function (data) {
                                    if (data.error_code == 0) {
                                        //删除成功移除当前行
                                        // _that.parents("tr").remove();
                                        //不移除当前行了，要展示恢复按钮
                                        history.go(0)
                                        //提示信息
                                        layer.msg('已删除!', {icon: 1, time: 1000});
                                    } else {
                                        layer.msg(data.msg);
                                    }

                                },
                                error: function (data) {
                                    console.log(data.msg);
                                },
                            });
                        });
                        return false;
                    }
                </script>
@endsection
